home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / DOLIST.CMM < prev    next >
Text File  |  1994-10-05  |  3KB  |  107 lines

  1. // *************************************************************
  2. // *** DoList.cmm - perform some action on every element     ***
  3. // *** ver.1        in a list.  The list is taken from       ***
  4. // ***              standard input or if a file is specified ***
  5. // ***              then from that file.                     ***
  6. // *************************************************************
  7.  
  8. #include <OptParms.lib>
  9.  
  10. #define INSERT_SEQUENCE  "$$$"
  11.  
  12. main(argc,argv)
  13. {
  14.    Skip = OptionalParameter(argc,argv,"SKIP",Temp) ? atoi(Temp) : 0 ;
  15.    Truncate = OptionalParameter(argc,argv,"TRUNC",Temp) ? atoi(Temp) : 0 ;
  16.    if ( argc < 2  ||  3 < argc
  17.      || OptionalParameter(argc,argv,"?")
  18.      || OptionalParameter(argc,argv,"help")
  19.      || OptionalParameter(argc,argv,"h") ) {
  20.       Instructions();
  21.       exit(EXIT_FAILURE);
  22.    }
  23.  
  24.    Action = argv[1];
  25.  
  26.    if ( argc == 2 ) {
  27.       // take input from stdin
  28.       fp = stdin;
  29.    } else {
  30.       fp = fopen(argv[2],"rt");
  31.       if ( !fp ) {
  32.          printf("\aUnable to open \"%s\" for reading.\n",argv[2]);
  33.          exit(EXIT_FAILURE);
  34.       }
  35.    }
  36.  
  37.    ActOnEachLine(Action,fp,Skip,Truncate);
  38.  
  39.    if ( argc != 2 )
  40.       fclose(fp);
  41. }
  42.  
  43. ActOnEachLine(Action,fp,Skip,Truncate)
  44. {
  45.    while( (line = fgets(fp)) ) {
  46.  
  47.       // get rid of neline at end
  48.       LineLen = strlen(line);
  49.       if ( line[LineLen-1] == '\n' )
  50.          line[--LineLen] == '\0';
  51.  
  52.       // remove Skip and Truncate text if applicable
  53.       if ( Skip <= LineLen ) {
  54.          line += Skip;
  55.          LineLen -= Skip;
  56.          if ( Truncate <= LineLen ) {
  57.             line[LineLen -= Truncate] = '\0';
  58.             if ( LineLen ) {
  59.  
  60.                ActOnLine(Action,line);
  61.             }
  62.          }
  63.       }
  64.    }
  65. }
  66.  
  67. ActOnLine(Action,Line)  // act on line, including INSERT_SEQUENCE
  68. {
  69.    // create statement replacing INSERT_SEQUENCE with line, or putting
  70.    // line at end if no INSERT_SEQUENCE
  71.    strcpy(lCmd,Action);
  72.  
  73.    // If insert sequence then replace with Line, else concatenate line
  74.    if ( insert = strstr(lCmd,INSERT_SEQUENCE) ) {
  75.       strcpy(insert + strlen(Line),insert+strlen(INSERT_SEQUENCE));
  76.       memcpy(insert,Line,strlen(Line));
  77.    } else {
  78.       strcat(lCmd," ");
  79.       strcat(lCmd,Line);
  80.    }
  81.  
  82.    // show and execute the command
  83.    printf("%s\n",lCmd);
  84.    system(lCmd);
  85. }
  86.                // finally, line is text to act upon
  87.  
  88.  
  89. Instructions()
  90. {
  91.    puts("\a")
  92.    puts(`DoList - Perform command on every element in a list`)
  93.    puts(``)
  94.    puts(`SYNTAX CEnviD DoList <Command> [ListFile] [/SKIP skip] [/TRUNC trunc]`)
  95.    puts(``)
  96.    puts(`WHERE: Command - Command to perform on each element in list, this may contain`)
  97.    puts(`                 the insert sequence ` INSERT_SEQUENCE ` for list element`)
  98.    puts(`       ListFile - File specification where each line is list element`)
  99.    puts(`       skip - skip this many characters from each line in list`)
  100.    puts(`       trunc - trancate this many character off the end of each item`)
  101.    puts(``)
  102.    puts(`EXAMPLES: CEnviD DoList PRINT MyList.lst`)
  103.    puts(`          DIR /b *.TXT | CEnviD DoList "type ` INSERT_SEQUENCE ` >> ALL.TXT"`)
  104.    puts(`          GREP -li NOMBAS * | CEnviD DoList "attrib +r" /SKIP 5 /TRUNC 1`)
  105. }
  106.  
  107.